﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WEALTH
{
    public partial class Product : ClientBasePage
    {
        private ExcuteDao proManage = new ExcuteDao();
        private string isall = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["UrlType"] != null && Request.QueryString["UrlType"].Equals("menu"))
            {
                Session["SortExpression_proList"] = null;
                Session["SortDirection_proList"] = null;

                Session["drpIssuanceAgency"] = null;
                Session["drpClass"] = null;
                Session["drpInvestmentIndustry"] = null;
                Session["drpExpectedReturnOnTime"] = null;
                Session["drpDeadline"] = null;
                Session["drpState"] = null;
                Session["drpTZMK"] = null;
                Session["txt_Key"] = null;
            }
            if (Request.QueryString["isall"] != null && Request.QueryString["isall"].Equals("ok"))
            {
                isall = "ok";
            }
            if (!string.IsNullOrEmpty(Request.QueryString["pageIndex"]))
            {
                string pageIndex = Request.QueryString["pageIndex"];
                int index = Convert.ToInt32(pageIndex);
                gvproList.PageIndex = index;

                Session["proListIndex"] = index;

                setSessionData();
            }
            //设置分页状态
            if (Session["SortExpression_proList"] != null)
            {
                this.gvproList.Attributes["SortExpression_proList"] = Session["SortExpression_proList"].ToString();
                this.gvproList.Attributes["SortDirection_proList"] = Session["SortDirection_proList"].ToString();
                setSortColumnStyle(this.gvproList, Session["SortExpression_proList"].ToString(), Session["SortDirection_proList"].ToString());
            }

            if (!IsPostBack)
            {
                ViewState["proAll"] = proManage.Get_ProductAll(drpState.SelectedItem.Text,
                                                drpIssuanceAgency.SelectedItem.Text,
                                                drpDeadline.SelectedValue,
                                                drpExpectedReturnOnTime.SelectedValue,
                                                drpTZMK.SelectedValue,
                                                drpInvestmentIndustry.SelectedItem.Text,
                                                drpClass.SelectedItem.Text,
                                                SpecialCharHandler.HtmlEncode(txt_Key.Text), isall);

                SearchproBindData();
            }
        }

        private void setSessionData()
        {
            //检索条件记录

            if (Session["drpIssuanceAgency"] != null)
            {
                drpIssuanceAgency.Text = Session["drpIssuanceAgency"].ToString();
            }

            if (Session["drpClass"] != null)
            {
                drpClass.Text = Session["drpClass"].ToString();
            }

            if (Session["drpInvestmentIndustry"] != null)
            {
                drpInvestmentIndustry.Text = Session["drpInvestmentIndustry"].ToString();
            }

            if (Session["drpExpectedReturnOnTime"] != null)
            {
                drpExpectedReturnOnTime.Text = Session["drpExpectedReturnOnTime"].ToString();
            }

            if (Session["drpDeadline"] != null)
            {
                drpDeadline.Text = Session["drpDeadline"].ToString();
            }

            if (Session["drpState"] != null)
            {
                drpState.Text = Session["drpState"].ToString();
            }

            if (Session["drpTZMK"] != null)
            {
                drpTZMK.Text = Session["drpTZMK"].ToString();
            }

            if (Session["txt_Key"] != null)
            {
                txt_Key.Text = SpecialCharHandler.HtmlDiscode(Session["txt_Key"].ToString());
            }

            if (Session["proListIndex"] != null)
            {
                gvproList.PageIndex = (int)Session["proListIndex"];
            }
        }
        #region 信息列表事件

        /// <summary>
        /// 信息列表数据加载时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvproList_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("id", e.Row.RowIndex.ToString());
                e.Row.Attributes.Add("onKeyDown", "SelectRow();");
                e.Row.Attributes.Add("onClick", "MarkRow(" + e.Row.RowIndex.ToString() + ");");
                //((HyperLink)e.Row.Cells[0].Controls[0]).NavigateUrl = "../Customer/EditChanpin.aspx?CPID="
                //    + SpecialCharHandler.HtmlDiscode(((HyperLink)e.Row.Cells[0].Controls[0]).Text).Replace("&", "amp;");

                //e.Row.Cells[1].Text = SpecialCharHandler.HtmlDiscode(e.Row.Cells[1].Text);
                ((HyperLink)e.Row.Cells[1].Controls[0]).NavigateUrl = "ProductDetail.aspx?CPID=" + e.Row.Cells[0].Text;
                string cpname = SpecialCharHandler.HtmlDiscode(((HyperLink)e.Row.Cells[1].Controls[0]).Text);
                ((HyperLink)e.Row.Cells[1].Controls[0]).ToolTip = cpname;
                if (cpname.Length > 15)
                {
                    cpname = cpname.Substring(0, 15) + "...";
                }
                ((HyperLink)e.Row.Cells[1].Controls[0]).Text = cpname;

                e.Row.Cells[5].Text = e.Row.Cells[5].Text + "万元";
                e.Row.Cells[6].Text = e.Row.Cells[6].Text + "个月";
                e.Row.Cells[0].Visible = false;
            }
            else
            {
                e.Row.Cells[0].Visible = false;
            }
        }
        #endregion

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            SearchproBindData();
        }

        /// <summary>
        /// 搜索按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ViewState.Clear();
            ViewState["proAll"] = proManage.Get_ProductAll(drpState.SelectedItem.Text,
                                                drpIssuanceAgency.SelectedItem.Text,
                                                drpDeadline.SelectedValue,
                                                drpExpectedReturnOnTime.SelectedValue,
                                                drpTZMK.SelectedValue,
                                                drpInvestmentIndustry.SelectedItem.Text,
                                                drpClass.SelectedItem.Text,
                                                SpecialCharHandler.HtmlEncode(txt_Key.Text),isall);
            //清空分页状态
            Session["SortExpression_proList"] = null;
            Session["SortDirection_proList"] = null;

            //检索条件记录
            Session["drpIssuanceAgency"] = drpIssuanceAgency.Text;
            Session["drpClass"] = drpClass.Text;
            Session["drpInvestmentIndustry"] = drpInvestmentIndustry.Text;
            Session["drpExpectedReturnOnTime"] = drpExpectedReturnOnTime.Text;
            Session["drpDeadline"] = drpDeadline.Text;
            Session["drpState"] = drpState.Text;
            Session["drpTZMK"] = drpTZMK.Text;
            Session["txt_Key"] = SpecialCharHandler.HtmlEncode(txt_Key.Text);

            SearchproBindData();

        }

        /// <summary>
        /// 选择页面功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvproList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            // 得到该控件
            GridView theGrid = sender as GridView;
            int newPageIndex = 0;
            if (e.NewPageIndex == -3)
            {
                //点击了Go按钮
                TextBox txtNewPageIndex = null;

                //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
                GridViewRow pagerRow = theGrid.BottomPagerRow;

                if (pagerRow != null)
                {
                    //得到text控件
                    txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
                }
                if (txtNewPageIndex != null)
                {
                    //得到索引
                    newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
                }
            }
            else
            {
                //点击了其他的按钮
                newPageIndex = e.NewPageIndex;
            }
            //防止新索引溢出
            newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
            newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

            //得到新的值
            theGrid.PageIndex = newPageIndex;

            //重新绑定
            SearchproBindData();
            //this.gvproList.PageIndex = e.NewPageIndex;
            //pageIndex = e.NewPageIndex;
            //Session["proListIndex"] = pageIndex;
            //SearchproBindData();
        }

        /// <summary>
        /// GridView排序事件
        /// </summary>
        protected void gvproList_Sorting(object sender, GridViewSortEventArgs e)
        {
            // 从事件参数获取排序数据列
            string SortExpression_proList = e.SortExpression.ToString();
            //// 假定为排序方向为“顺序”
            string SortDirection_proList = "ASC";
            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (SortExpression_proList == this.gvproList.Attributes["SortExpression_proList"])
            {
                //获得下一次的排序状态
                SortDirection_proList = (this.gvproList.Attributes["SortDirection_proList"].ToString() == SortDirection_proList ? "DESC" : "ASC");
            }
            // 重新设定GridView排序数据列及排序方向
            this.gvproList.Attributes["SortExpression_proList"] = SortExpression_proList;
            this.gvproList.Attributes["SortDirection_proList"] = SortDirection_proList;
            //记录分页状态
            Session["SortExpression_proList"] = SortExpression_proList;
            Session["SortDirection_proList"] = SortDirection_proList;

            setSortColumnStyle(this.gvproList, SortExpression_proList, SortDirection_proList);
            this.SearchproBindData();
        }

        /// <summary>
        /// 绑定到GridView
        /// </summary>
        private void SearchproBindData()
        {
            // 获取GridView排序数据列及排序方向
            string SortExpression_proList = this.gvproList.Attributes["SortExpression_proList"];
            string SortDirection_proList = this.gvproList.Attributes["SortDirection_proList"];
            // 调用业务数据获取方法
            DataTable dtBind = (DataTable)ViewState["proAll"];

            //页面数据总数
            recordCount = dtBind.Rows.Count;

            // 根据GridView排序数据列及排序方向设置显示的默认数据视图
            if ((!string.IsNullOrEmpty(SortExpression_proList)) && (!string.IsNullOrEmpty(SortDirection_proList)))
            {
                dtBind.DefaultView.Sort = string.Format("{0} {1}", SortExpression_proList, SortDirection_proList);
            }

            AspNetPager1.PageSize = 10;
            //初始化分页数据源实例            
            PagedDataSource pds = new PagedDataSource();
            //设置总行数           
            AspNetPager1.RecordCount = dtBind.Rows.Count;
            //设置分页的数据源          
            pds.DataSource = dtBind.DefaultView;
            //设置当前页         
            pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            //设置每页显示页数           
            pds.PageSize = AspNetPager1.PageSize;
            //启用分页         
            pds.AllowPaging = true;
            //设置GridView的数据源为分页数据源          
            gvproList.DataSource = pds;

            // GridView绑定并显示数据
            //this.gvproList.DataSource = dtBind;
            this.gvproList.DataBind();

            pageCount = gvproList.PageCount;
            pageIndex = gvproList.PageIndex;

        }

        protected void gvproList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            //int PageSize = this.gvproList.PageSize;
            //int PageIndex = this.gvproList.PageIndex;
            //int PageCount = this.gvproList.PageCount;
            //int RecoderCount = this.gvproList.Rows.Count;

            //if (e.Row.RowType == DataControlRowType.Pager)
            //{
            //    LinkButton Frist = new LinkButton();
            //    LinkButton Prev = new LinkButton();
            //    LinkButton Next = new LinkButton();
            //    LinkButton Last = new LinkButton();
            //    e.Row.Controls.Clear();
            //    TableCell tc = new TableCell();
            //    tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
            //    tc.Controls.Add(new LiteralControl("共" + RecoderCount.ToString() + "条记录"));
            //    tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
            //    tc.Controls.Add(new LiteralControl("共" + PageCount.ToString() + "页"));
            //    tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
            //    tc.Controls.Add(new LiteralControl("当前第" + (PageIndex + 1).ToString() + "页"));
            //    tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
            //    tc.Controls.Add(new LiteralControl("每页" + PageSize.ToString() + "条记录"));
            //    tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;"));

            //    Frist.Text = "首页";
            //    Frist.CommandName = "Page";
            //    Frist.CommandArgument = "First";
            //    Frist.Font.Underline = false;

            //    Prev.Text = "上一页";
            //    Prev.CommandName = "Page";
            //    Prev.CommandArgument = "Prev";
            //    Prev.Font.Underline = false;

            //    Next.Text = "下一页";
            //    Next.CommandName = "Page";
            //    Next.CommandArgument = "Next";
            //    Next.Font.Underline = false;

            //    Last.Text = "尾页";
            //    Last.CommandName = "Page";
            //    Last.CommandArgument = "Last";
            //    Last.Font.Underline = false;

            //    if (PageIndex <= 0)
            //    {
            //        Prev.Enabled = false;
            //        Frist.Enabled = false;
            //    }
            //    else
            //    {
            //        Prev.Enabled = true;
            //        Frist.Enabled = true;
            //    }
            //    tc.Controls.Add(Frist);
            //    tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
            //    tc.Controls.Add(Prev);
            //    tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));

            //    for (int i = 0; i < PageCount; i++)
            //    {
            //        if (i == PageIndex)
            //        {
            //            tc.Controls.Add(new LiteralControl(" <span style='color:red;font-weight:bold'>" + (i + 1).ToString() + " </span>"));

            //        }
            //        else
            //        {
            //            LinkButton lbBtn = new LinkButton();
            //            lbBtn.Text = (i + 1).ToString();
            //            lbBtn.CommandName = "Page";
            //            lbBtn.CommandArgument = (i + 1).ToString();
            //            lbBtn.Font.Underline = false;
            //            tc.Controls.Add(lbBtn);
            //        }
            //        tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
            //    }
            //    if (PageIndex >= PageCount)
            //    {
            //        Next.Enabled = false;
            //        Last.Enabled = false;
            //    }
            //    else
            //    {
            //        Next.Enabled = true;
            //        Last.Enabled = true;
            //    }
            //    tc.Controls.Add(Next);
            //    tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
            //    tc.Controls.Add(Last);
            //    tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));

            //    e.Row.Controls.Add(tc);
            //}
            //if (gvproList.Rows.Count != 0)
            //{
            //    Control table = gvproList.Controls[0];
            //    int count = table.Controls.Count;
            //    table.Controls[count - 1].Visible = true;
            //}
        }
    }
}